package imoney.shared.model;


import imoney.shared.Statics;

import java.io.Serializable;
import java.util.ArrayList;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;


/**
 * shared class used to store data about users :
 * String userID, String pseudo, ArrayList<Integer> groups, and int currentGroup
 */
@PersistenceCapable
public class Person extends Registreable implements Serializable {
	/**
	 * Date of the last versioned update.
	 */
	private static final long serialVersionUID = 05062011L;
	
	@Persistent
	private String userID;
	@Persistent
	private String pseudo;
	@Persistent
	private ArrayList<String> groups;
	@Persistent
	private String currentGroup;
	// @Persistent
	// TODO : private ArrayList<Message> messages;
	
	public Person(String userid, String pseud, ArrayList<String> list, String currentgp){
		userID = userid;
		pseudo = pseud;
		groups = list;
		currentGroup = currentgp;
	}
	
	public Person(String googleID, String pseud){
		this(googleID, pseud, new ArrayList<String>(), "No group");
	}
	
	public Person(String googleID){
		this(googleID, Statics.DEFAULT_PSEUDO);
	}
	
	/**
	 * Void constructor required by the Persistence API. Use rather the
	 * Person(String userID, String pseudo) constructor when registering
	 * a new member.
	 */
	public Person(){
		this("arnaudDev");
	}
	
	// GETTERS ------------------------------------------------

	public String getUserID(){
		return userID;
	}
	public String getPseudo(){
		return pseudo;
	}
	public ArrayList<String> getGroupsList(){
		return groups;
	}
	public String getLastGroup(){
		return currentGroup;
	}
	
	// SETTERS --------------------------------------------------
	public void setUserId(String userid){
		userID = userid;
	}
	
	public void setPseudo(String pseud){
		pseudo=pseud;
	}
	
	/**
	 * Remember the last active group of the person to load it next time
	 * in the UI.
	 */
	public void setLastGroup(String name){
		if( groups.indexOf(name) > -1){
			currentGroup = name;
		}
		else{
			System.out.println("Problem with Person.setLastGroup(), group unfound in person list : "+name);
		}
	}

	
	/**
	 * As a user might already have a list of groups he's in, it could be harmfull to use this method that replaces
	 * the existing list by the given parameter. By security, for now if the existing list ain't empty, method will do nothing
	 * @param list : the list of groups this person is in (recorded with their proper integer identifier).
	 */
	@Deprecated
	public void setGroups(ArrayList<String> list){
		if(groups.isEmpty()){
			groups = list;
		}
	}
	// METHODS ------------------------------------
	/**
	 * Append the group to the list of this <code>Person</code>.
	 * If the group is already in the list, do nothing.
	 */
	public void addGroup(String groupName){
		if(groups.indexOf(groupName) == -1){
			groups.add(groupName);
		}
	}
	
	/**
	 * Removes the first occurence of the group found
	 * corresponding to this name in the <code>Person</code> list.
	 * @param gpName, the name of the group to remove from the <code>Person</code> list.
	 */
	public void deleteGroup(String gpName){
		groups.remove(gpName);
	}
	@Override
	public String toString(){
		return super.toString()+", userID : "+userID+", Pseudo : "+pseudo+", Groups : "+groups+", LastGroup : "+currentGroup;
	}

	@Override
	public String getAvailabilityQuery() {
		return "select from "+Person.class.getName()+" where userID=='"+userID+"'";
	}

	@Override
	public String getAvailabilityExceptionMessage() {
		return "The userID '"+getUserID()+"' is already in use.";
	}
	
	/**
	 * The equality is only based on the userID.
	 */
	@Override
	public boolean equals(Object o) {
		try {
			Person pers = (Person) o;
			return userID.equals(pers.getUserID());
		}
		catch(Exception ex) {
			return false;
		}
	}


}